/**
 * @class service.point
 * @this service.point
 * @classdesc 积分服务
 * @type {*|exports|module.exports}
 */
var util = require('../util'),
    weixinuserDao   = require('../db').weixinuser,
    Promise  = require('bluebird'),
    https = require('https'),
    config = require('../util').config,
    sandbox = util.sandbox;

sandbox.on(sandbox.MSG.AWARD_REGISTER_QRCODE, awardRegisterQrcode);
/**
 * 积分奖励: A用户 做X动作 导致 B用户积分从新计算(根据action_type不同采取不同的积分算法)
 * @param userid 动作发起用户
 * @param refenceUserId 积分变化用户
 * @param action_type 动作类型
 * @returns {Promise.<T>|*}
 */
function award (userInfo, refencePromotionCode, actionType) {
  var promise;
  switch (actionType) {
    case 'subscribe_via_qrcode':
      promise = awardSubscribeQrcode(userInfo, refencePromotionCode, actionType);
      break;
    default :
      promise = Promise.resolve();
      break;
  }
  return promise;
}

/**
 *
 * @param userInfo
 * @returns {Promise|*}
 */
function awardSubscribeQrcode (userInfo, refencePromotionCode, actionType) {
  return weixinuserDao.findOne({promotion_code: refencePromotionCode}).then(function (doc) {
    if (doc) {
      doc.point = doc.point + config.award_subscribe_qrcode;
      return Promise.resolve(doc.save())
        .then(util.sandbox.emit.bind(util.sandbox, util.sandbox.MSG.LOG_POINTHISTORY, generatePointHistory(userInfo, doc, actionType)))

        // TODO 目前的策略是：奖励后全部兑换成vpn时间
        .delay(100)
        .then(util.sandbox.emit.bind(util.sandbox, util.sandbox.MSG.EXCH_POINT_TO_VPN_TIME, {openid:doc.openid, dates: 999}));
    } else {
      return Promise.resolve({});
    }
  }).catch(util.log.log2Error);
}

function awardRegisterQrcode (msg) {
  var userInfo = msg.trigger,
      referencePromotionCode = msg.referenceCode,
      actionType = msg.actionType;
  var award = getPointByActionType(actionType);
  return weixinuserDao.findOne({promotion_code: referencePromotionCode}).then(function (reference) {
    if (reference) {
      reference.point = reference.point + award;
      return Promise.resolve(reference.save())
        .then(util.sandbox.emit.bind(util.sandbox, util.sandbox.MSG.LOG_POINTHISTORY, generatePointHistory(userInfo, reference, actionType)))

        // TODO 目前的策略是：奖励后全部兑换成vpn时间
        .delay(100)
        .then(util.sandbox.emit.bind(util.sandbox, util.sandbox.MSG.EXCH_POINT_TO_VPN_TIME, {openid:reference.openid, dates: 999}));
    } else {
      return Promise.resolve({});
    }
  }).catch(util.log.log2Error);
}

function generatePointHistory (triggeredBy, awardedTo, actionType, exchange) {
  var pointHistory = {
    triggered_by: triggeredBy.openid,
    triggered_by_name: triggeredBy.nickname,
    action_type: actionType,
    awarded_to: awardedTo.openid,
    awarded_to_name: awardedTo.nickname,
    create_time: Date.now(),
    point: getPointByActionType(actionType)
  }

  if (exchange && exchange.exchange_type && exchange.amount) {
    pointHistory['exchange'] = exchange;
  }
  return pointHistory;
}

function getPointByActionType (actionType) {
  switch (actionType) {
    case 'subscribe_via_qrcode':
      return config.award_subscribe_qrcode;
      break;
    case 'register':
      return config.award_register_qrcode;
      break;
    default :
      return 0;
      break;
  }
}

module.exports = {
  award: award
};